<?php
namespace cesi\plastprodBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
use cesi\plastprodBundle\Entity\Password;
use cesi\plastprodBundle\Entity\Picture;
use cesi\plastprodBundle\Form\PasswordType;
use cesi\plastprodBundle\Form\PictureType;
use Symfony\Component\HttpFoundation\Response;

class SecurityController extends Controller
{
    public function loginAction()
    {
        $request = $this->getRequest();
        $session = $request->getSession();

        // get the login error if there is one
        if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
            $error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
        } else {
            $error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
        }
        
    }

	public function changePasswordAction() {
        $request = $this->getRequest();
        $password = new Password();
        $form = $this->createForm(new PasswordType(), $password);
        $form->bindRequest($request);
        $message = "";
        
        if ($request->getMethod() == 'POST') 
        {
            $old= $password->getOld();
            $new = $password->getNew();
            $conf = $password->getConfirmation();
            
            $user = $this->get('security.context')->getToken()->getUser();

            if(is_object($user)) {
                $userOldPassword = $user->getPassword();
                
                $factory = $this->container->get('security.encoder_factory');
                $encoder = $factory->getEncoder($user);
                $old = $encoder->encodePassword($old, $user->getSalt());
                
                if($old == $userOldPassword) {
                    if($new == $conf) {
                        $newPassword = $encoder->encodePassword($new, $user->getSalt());
                        $user->setUserPassword($newPassword);

                        $em = $this->get('doctrine.orm.entity_manager');
                        $em->persist($user);
                        $em->flush();
                        $message = "OK";
                    } else {
                        $message = "Le nouveau mot de passe et sa confirmation sont différents.";
                    }
                } else {
                    
                    $message = 'L\'ancien mot de passe ne correspond pas.';
                }
            }
        }
        return new Response($message);
//        return $this->render('OweimMainBundle:Security:changepassword.html.twig', array(
//            'message' => $message
//        ));
    }
    
    public function changeEmailAction() {
        $request = $this->getRequest();
        $newEmail = $request->query->get('emailValue');
        
        $user = $this->get('security.context')->getToken()->getUser();

        if(is_object($user)) {
            if ( preg_match("[^([a-zA-Z0-9]+(([\.\-\_]?[a-zA-Z0-9]+)+)?)\@(([a-zA-Z0-9]+[\.\-\_])+[a-zA-Z]{2,4})$]", $newEmail)){
                $user->setUserMail($newEmail);
                $em = $this->get('doctrine.orm.entity_manager');
                $em->persist($user);
                $em->flush();
            }else{
                return new Response('L\'email n\'est pas un email valide.');
            }
        }
        
        return new Response('OK');
    }

 	public function changeFirstNameAction() {
        $request = $this->getRequest();
        $newFName = $request->query->get('fnameValue');
        
        $user = $this->get('security.context')->getToken()->getUser();

        if(is_object($user)) {
            $user->setUserFirstName($newFName);
            $em = $this->get('doctrine.orm.entity_manager');
            $em->persist($user);
            $em->flush();
        }
        
        return new Response('OK');
    }

 	
    public function changeLastNameAction() {
        $request = $this->getRequest();
        $newLName = $request->query->get('lnameValue');
        
        $user = $this->get('security.context')->getToken()->getUser();

        if(is_object($user)) {
            $user->setUserLastName($newLName);
            $em = $this->get('doctrine.orm.entity_manager');
            $em->persist($user);
            $em->flush();
        }
        
        return new Response('OK');
    }
     
}
